home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.os.msdos.programmer,comp.lang.c
- Subject: Re: open vs fopen?
- Date: Thu, 22 Feb 96 23:28:36 GMT
- Organization: none
- Message-ID: <825031716snz@genesis.demon.co.uk>
- References: <uEYFxc9nX8WX083yn@mbnet.mb.ca> <4f8qbg$549@chleuasme.francenet.fr> <4gg5nk$he1@salyko.cube.net>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4gg5nk$he1@salyko.cube.net>
- medardus@cube.net "Andreas Karnetzki" writes:
-
- >sherlock@micronet.fr schrieb:
- >> natewild@mbnet.mb.ca (Nathan T. Wild) wrote:
- >
- >> >In C, why would one use open and DOS int handles rather than fopen and
- >> >stdio file handles?
- >
- >> >Is there some speed advantage or differnet functionality?
- >
- >
- >> because files opened using open and DOS files handles can be read and
- >> write at the same time, while stdio files can't.
- >
- >That's not correct, I think. You can open files for read/write with the
- >the stdio routines, too. Just use the "w+", "r+" or "a+" open modes with
- >fopen().
- >
- > fopen( "New.dat", "w+" );
- >
- >opens a file starting with 0 bytes for writing but with the possibility
- >to switch to read mode without the need of closing it.
- >
- > fopen( "Old.dat", "r+" );
- >
- >opens an (existing) file starting with reading with the possibility to
- >switch to writing without closing.
- >
- > fopen( "Any.dat", "a+" );
- >
- >opens for appending and maybe reading afterwards.
- >
- >The only thing to assure for read/write switches is to use an
- >fseek() call between read and write operations:
- >
- > fp = fopen( "My.dat", "r+" );
- > fwrite( "Hello", 1, 5, fp );
- > fseek( fp, 0L, SEEK_SET );
-
- You could have fflush(), rewind() or fsetpos() instead here.
-
- > fread( Message, 1, 5, fp );
- > fseek( fp, 0L, SEEK_CUR );
-
- You could have rewind() or fsetpos() instead here.
-
- > fwrite( " and Goodby.", 1, 12, fp );
- > fclose( fp );
-
- >The main difference between stdio calls and the "low-level" routines
- >(not only under DOS but for Unix too) is the absence of buffering in
- >the low-level functions.
-
- The 'low-level' functions typically have buffering in the OS. However
- stdio typically implements an extra level of buffering local to the
- particular program.
-
- >if you use fwrite(), for example, you can not be sure that the number of
- >bytes you want to write are actually written to the output at that time.
- >The "real" write operation will take place if the stdio buffer for that
- >file is full.
-
- A good implementation of fwrite could bypass the stdio buffering where the
- block sizes were appropriate.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-